﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor.Widgets.MindMap
{
    public class CommentAdorner : Adorner
    {
        /// <summary>
        /// 用在标题备注文本显示时的装饰器。
        /// </summary>
        /// <param name="adornedElement">应传入备注文本块。</param>
        public CommentAdorner(MindMapTitle masterTitle, UIElement adornedElement)
            : base(adornedElement)
        {
            commentTextBlock = adornedElement as TextBlock;
            this.masterTitle = masterTitle;
            //this.Opacity = 0.5;

            //终于解决了选取装饰器中线条宽度为2而不是1像素的问题！！！
            this.SnapsToDevicePixels = true;
            this.SetValue(RenderOptions.EdgeModeProperty, EdgeMode.Aliased);
        }

        private MindMapTitle masterTitle;

        private TextBlock commentTextBlock;

        protected override void OnRender(DrawingContext drawingContext)
        {
            if (masterTitle == null) return;

            Brush renderBrush = commentTextBlock.Foreground;
            Pen renderPen     = new Pen(commentTextBlock.Foreground, 1);
            double fontSize   = commentTextBlock.FontSize;
            double ptaY       = commentTextBlock.ActualHeight / 2;
            
            //if (Application.Current.MainWindow.View != ViewMode.Default || masterTitle.Direction == TitleDirection.Right)//只有导图会分左右。
            //{
            //右向箭头
            Rect newRect              = new Rect(-fontSize, ptaY - fontSize / 2, fontSize * 0.8, fontSize);
            double width              = newRect.Width;
            double height             = newRect.Height;
            double halfHeight         = height / 2;
            int quarterHeight         = (int)height / 4;
            Point pta                 = new Point(newRect.Right, ptaY);
            Point ptb                 = new Point(newRect.BottomRight.X - halfHeight, newRect.Bottom);
            Point ptc                 = new Point(newRect.TopRight.X - halfHeight, pta.Y + quarterHeight);
            Point ptd                 = new Point(newRect.Left, ptc.Y);
            Point pte                 = new Point(ptd.X, pta.Y - quarterHeight + 1);
            Point ptf                 = new Point(ptb.X, pte.Y);
            Point ptg                 = new Point(ptb.X, newRect.Top);
            PathGeometry pathGeometry = new PathGeometry();
            PathFigure pf             = new PathFigure() { StartPoint = pta, IsClosed = true, IsFilled = true };
            PolyLineSegment pls       = new PolyLineSegment(
                new List<Point>()
                {
                    pta,ptb,ptc,ptd,pte,ptf,ptg,pta,
                }, true);
            pf.Segments.Add(pls);
            pathGeometry.Figures.Add(pf);

            drawingContext.DrawGeometry(renderBrush, renderPen, pathGeometry);
            //}
            //else
            //{
            //左向箭头
            //Rect rect = new Rect(this.AdornedElement.RenderSize);
            //Rect newRect = new Rect(rect.Right + fontSize * 0.2, ptaY - fontSize / 2, fontSize * 0.8, fontSize);

            //double width = newRect.Width;
            //double height = newRect.Height;
            //double halfHeight = height / 2;
            //int quarterHeight = (int)height / 4;

            //Point pta = new Point(newRect.Left, ptaY);
            //Point ptb = new Point(newRect.Left + halfHeight, newRect.Bottom);
            //Point ptc = new Point(ptb.X, pta.Y + quarterHeight);
            //Point ptd = new Point(newRect.Right, ptc.Y);
            //Point pte = new Point(ptd.X, pta.Y - quarterHeight + 1);
            //Point ptf = new Point(ptb.X, pte.Y);
            //Point ptg = new Point(ptb.X, newRect.Top);

            //PathGeometry pathGeometry = new PathGeometry();
            //PathFigure pf = new PathFigure() { StartPoint = pta, IsClosed = true, IsFilled = true };
            //PolyLineSegment pls = new PolyLineSegment(
            //    new List<Point>()
            //{
            //    pta,ptb,ptc,ptd,pte,ptf,ptg,pta,
            //}, true);
            //pf.Segments.Add(pls);
            //pathGeometry.Figures.Add(pf);

            //drawingContext.DrawGeometry(renderBrush, renderPen, pathGeometry);
            //}
        }
    }
}